<template>
  <div class="contract-business">
    <a-card title="生成业务">
      <div slot="extra">
        <!--          <a-button type="danger" style="margin-right:5px" @click="$router.push({ name: 'BusinessList', params: { type: 'status2_1' } })"><a-icon type="eye"></a-icon>已开启材料未提交业务</a-button>-->
        <a-button icon="plus" :disabled="query.status == 20 || query.status == 30 || query.status == 34 || query.status == 40 || query.status == 44" type="primary" @click="businessModal">
          添加业务
        </a-button>
      </div>
      <a-table
        bordered
        size="middle"
        :row-key="(record) => record.cb_id"
        :columns="selectedColumns"
        :data-source="selectedData"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :pagination="false"
        :scroll="{ y: '600px' }"
      >
        <div slot="title">
          <a-alert show-icon type="info">
            <template slot="message">
              总官费=基础官费+附加官费; | 代理费=总收款-总官费;
            </template>
          </a-alert>
        </div>
        <template slot="contract_index" slot-scope="text, record, index">
          {{ index + 1 }}
        </template>
        <template slot="price" slot-scope="text,record">
          <a-button type="link" icon="edit" @click="batchFee(1, record.cb_id)">{{text}}</a-button>
        </template>
        <template slot="status" slot-scope="text">
          <a-tag :color="text == 20 ? 'blue' : (text == 10 ? '' : 'red')">{{ statusOpts[text] }}</a-tag>
        </template>
        <template slot="additional_content" slot-scope="text, record">
          <div v-if="record.exts.length == 0">
            <a-tag color="red">未添加附加项</a-tag>
          </div>
          <div v-else>
            <span>1、{{record.exts[0].name}} {{record.exts[0].amount}}</span>
            <a-button type="link" @click="onExtView(record.exts)">查看</a-button>
          </div>
        </template>
        <template slot="action" slot-scope="text, record">
          <a-button size="small" :disabled="record.status1 == 20" type="danger" @click="onDel(record.cb_id)">
            删除
          </a-button>
        </template>
        <template slot="footer">
          <a-row>
            <a-col :span="12">
              <div style="text-align: left">
                <a-button
                  icon="setting"
                  :disabled="selectedRowKeys.length === 0"
                  size="small"
                  type="primary"
                  @click="batchFee(2,selectedRowKeys)"
                  style="margin-right:5px"
                >
                  批量修改收款
                </a-button>
<!--                <a-button disabled :loading="loading3" icon="mail" size="small" type="danger" @click="sendSms">发送确认合同短信至客户</a-button>-->
                <a-button disabled :loading="loading3" icon="mail" size="small" type="danger">发送确认合同短信至客户</a-button>
              </div>
            </a-col>
            <a-col :span="12">
              <div style="text-align: right">
                收款: {{totalAmount}}
                <a-divider type="vertical" />
                基础官费总计: {{totalOfficial}}
                <a-divider type="vertical" />
                附加项官费: {{totalExt}}
                <a-divider type="vertical" />
                总官费: {{totalAllOff}} = ({{totalOfficial}} + {{totalExt}})
                <a-divider type="vertical" />
                代理费: {{totalAgency}}
              </div>
            </a-col>
          </a-row>
        </template>
      </a-table>
    </a-card>
    <a-modal
      centered
      title="选择业务"
      destroy-on-close
      :maskClosable="false"
      :width="1200"
      v-model="visible"
      @ok="onSubBusiness"
      :confirm-loading="confirmLoading"
      @cancel="() => this.seletedData = []"
    >
      <a-form-model>
        <a-form-model-item>
          <a-alert type="error" message="如业务添加/删除/修改出现错误，请关掉窗口再试!!!!!!!"></a-alert>
        </a-form-model-item>
        <a-form-model-item>
          <a-checkbox-group
            v-model="businessData3"
            @change="onChange"
          >
            <a-spin :spinning="loading">
              <a-tabs type="card">
                <a-button type="link" icon="eye" slot="tabBarExtraContent">
                  在线预览新老业务对比表
                </a-button>
                <a-tab-pane key="10" tab="商标">
                  <div class="business-checkbox-group">
                    <div
                      class="b_group"
                    >
                      <a-row :gutter="[0, 20]" v-for="item in sourceData[10]" :key="item.business_id">
                        <a-col :span="24">
                          <span class="group-name">{{ item.name }}</span>
                        </a-col>
                        <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                          <a-checkbox :value="items">
                            <a-tooltip v-if="items.name_desc != ''">
                              <template slot="title">
                                {{ items.name_desc }}
                              </template>
                              <ellipsis :length="30">{{ items.name }}</ellipsis>
                            </a-tooltip>
                            <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                          </a-checkbox>
                        </a-col>
                      </a-row>
                    </div>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="20" tab="专利">
                  <div class="business-checkbox-group">
                    <div
                      class="b_group"
                    >
                      <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[20]" :key="index">
                        <a-col :span="24">
                          <span class="group-name">{{ item.name }}</span>
                        </a-col>
                        <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                          <a-checkbox :value="items">
                            <a-tooltip v-if="items.name_desc != ''">
                              <template slot="title">
                                {{ items.name_desc }}
                              </template>
                              <ellipsis :length="30">{{ items.name }}</ellipsis>
                            </a-tooltip>
                            <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                          </a-checkbox>
                        </a-col>
                      </a-row>
                    </div>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="30" tab="版权">
                  <div class="business-checkbox-group">
                    <div
                      class="b_group"
                    >
                      <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[30]" :key="index">
                        <a-col :span="24">
                          <span class="group-name">{{ item.name }}</span>
                        </a-col>
                        <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                          <a-checkbox :value="items">
                            <a-tooltip v-if="items.name_desc != ''">
                              <template slot="title">
                                {{ items.name_desc }}
                              </template>
                              <ellipsis :length="30">{{ items.name }}</ellipsis>
                            </a-tooltip>
                            <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                          </a-checkbox>
                        </a-col>
                      </a-row>
                    </div>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="40" tab="设计">
                  <div class="business-checkbox-group">
                    <div
                      class="b_group"
                    >
                      <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[40]" :key="index">
                        <a-col :span="24">
                          <span class="group-name">{{ item.name }}</span>
                        </a-col>
                        <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                          <a-checkbox :value="items">
                            <a-tooltip v-if="items.name_desc != ''">
                              <template slot="title">
                                {{ items.name_desc }}
                              </template>
                              <ellipsis :length="30">{{ items.name }}</ellipsis>
                            </a-tooltip>
                            <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                          </a-checkbox>
                        </a-col>
                      </a-row>
                    </div>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="50" tab="国际">
                  <div class="business-checkbox-group">
                    <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[50]" :key="index">
                      <a-col :span="24">
                        <span class="group-name">{{ item.name }}</span>
                      </a-col>
                      <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                        <a-checkbox :value="items">
                          <a-tooltip v-if="items.name_desc != ''">
                            <template slot="title">
                              {{ items.name_desc }}
                            </template>
                            <ellipsis :length="30">{{ items.name }}</ellipsis>
                          </a-tooltip>
                          <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                        </a-checkbox>
                      </a-col>
                    </a-row>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="60" tab="高新">
                  <div class="business-checkbox-group">
                    <div
                      class="b_group"
                    >
                      <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[60]" :key="index">
                        <a-col :span="24">
                          <span class="group-name">{{ item.name }}</span>
                        </a-col>
                        <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                          <a-checkbox :value="items">
                            <a-tooltip v-if="items.name_desc != ''">
                              <template slot="title">
                                {{ items.name_desc }}
                              </template>
                              <ellipsis :length="30">{{ items.name }}</ellipsis>
                            </a-tooltip>
                            <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                          </a-checkbox>
                        </a-col>
                      </a-row>
                    </div>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="70" tab="资产">
                  <div class="business-checkbox-group">
                    <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[70]" :key="index">
                      <a-col :span="24">
                        <span class="group-name">{{ item.name }}</span>
                      </a-col>
                      <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                        <a-checkbox :value="items">
                          <a-tooltip v-if="items.name_desc != ''">
                            <template slot="title">
                              {{ items.name_desc }}
                            </template>
                            <ellipsis :length="30">{{ items.name }}</ellipsis>
                          </a-tooltip>
                          <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                        </a-checkbox>
                      </a-col>
                    </a-row>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="80" tab="ISO">
                  <div class="business-checkbox-group">
                    <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[80]" :key="index">
                      <a-col :span="24">
                        <span class="group-name">{{ item.name }}</span>
                      </a-col>
                      <a-col :span="12" v-for="items in item.data" :key="items.business_id">
                        <a-checkbox :value="items">
                          <a-tooltip v-if="items.name_desc != ''">
                            <template slot="title">
                              {{ items.name_desc }}
                            </template>
                            <ellipsis :length="80">【{{ epOpts[items.enterprise_persons] }}】【{{ atOpts[items.auth_type] }}】【{{ riOpts[items.risk] }}】{{ items.name }}</ellipsis>
                          </a-tooltip>
                          <ellipsis v-else :length="80">[{{ epOpts[items.enterprise_persons] }}][{{ atOpts[items.auth_type] }}][{{ riOpts[items.risk] }}]{{ items.name }}</ellipsis>
                        </a-checkbox>
                      </a-col>
                    </a-row>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="90" tab="国际商标">
                  <div class="business-checkbox-group">
                    <div
                      class="b_group"
                    >
                      <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[90]" :key="index">
                        <a-col :span="24">
                          <span class="group-name">{{ item.name }}</span>
                        </a-col>
                        <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                          <a-checkbox :value="items">
                            <a-tooltip v-if="items.name_desc != ''">
                              <template slot="title">
                                {{ items.name_desc }}
                              </template>
                              <ellipsis :length="30">{{ items.name }}</ellipsis>
                            </a-tooltip>
                            <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                          </a-checkbox>
                        </a-col>
                      </a-row>
                    </div>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="100" tab="国际专利">
                  <div class="business-checkbox-group">
                    <div
                      class="b_group"
                    >
                      <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[100]" :key="index">
                        <a-col :span="24">
                          <span class="group-name">{{ item.name }}</span>
                        </a-col>
                        <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                          <a-checkbox :value="items">
                            <a-tooltip v-if="items.name_desc != ''">
                              <template slot="title">
                                {{ items.name_desc }}
                              </template>
                              <ellipsis :length="30">{{ items.name }}</ellipsis>
                            </a-tooltip>
                            <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                          </a-checkbox>
                        </a-col>
                      </a-row>
                    </div>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="110" tab="3A">
                  <div class="business-checkbox-group">
                    <div
                      class="b_group"
                    >
                      <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[110]" :key="index">
                        <a-col :span="24">
                          <span class="group-name">{{ item.name }}</span>
                        </a-col>
                        <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                          <a-checkbox :value="items">
                            <a-tooltip v-if="items.name_desc != ''">
                              <template slot="title">
                                {{ items.name_desc }}
                              </template>
                              <ellipsis :length="30">{{ items.name }}</ellipsis>
                            </a-tooltip>
                            <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                          </a-checkbox>
                        </a-col>
                      </a-row>
                    </div>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="120" tab="400">
                  <div class="business-checkbox-group">
                    <div
                      class="b_group"
                    >
                      <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[120]" :key="index">
                        <a-col :span="24">
                          <span class="group-name">{{ item.name }}</span>
                        </a-col>
                        <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                          <a-checkbox :value="items">
                            <a-tooltip v-if="items.name_desc != ''">
                              <template slot="title">
                                {{ items.name_desc }}
                              </template>
                              <ellipsis :length="30">{{ items.name }}</ellipsis>
                            </a-tooltip>
                            <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                          </a-checkbox>
                        </a-col>
                      </a-row>
                    </div>
                  </div>
                </a-tab-pane>
                <a-tab-pane key="300" tab="其他">
                  <div class="business-checkbox-group">
                    <a-row :gutter="[0, 20]" v-for="(item, index) in sourceData[300]" :key="index">
                      <a-col :span="24">
                        <span class="group-name">{{ item.name }}</span>
                      </a-col>
                      <a-col :span="6" v-for="items in item.data" :key="items.business_id">
                        <a-checkbox :value="items">
                          <a-tooltip v-if="items.name_desc != ''">
                            <template slot="title">
                              {{ items.name_desc }}
                            </template>
                            <ellipsis :length="30">{{ items.name }}</ellipsis>
                          </a-tooltip>
                          <ellipsis v-else :length="30">{{ items.name }}</ellipsis>
                        </a-checkbox>
                      </a-col>
                    </a-row>
                  </div>
                </a-tab-pane>
              </a-tabs>
              <a-divider />
              <s-table
                bordered
                ref="table"
                size="middle"
                :row-key="(record) => record.business_id"
                :columns="businessColumns"
                :data="businessData2"
              >
                <div slot="title">
                  <a-alert show-icon type="info">
                    <template slot="message">
                      总官费=基础官费+附加官费; | 代理费=总收款-总官费;
                    </template>
                  </a-alert>
                </div>
                <template slot="contract_index" slot-scope="text, record, index">
                  {{ index + 1 }}
                </template>
                <template slot="category" slot-scope="text">
                  {{ catOptions[text] }}
                </template>
<!--                <template slot="price" slot-scope="text, record">-->
<!--                  <a-input-number-->
<!--                    :default-value="record.price"-->
<!--                    :value="record.price"-->
<!--                    @change="e => handleChange(e, record.business_id, record.cache_id)"-->
<!--                  />-->
<!--                </template>-->
                <template slot="num" slot-scope="text,record">
                  <a-input-number
                    :default-value="record.num"
                    :value="record.num"
                    :min="1"
                    @change="e => handleChange2(e, record.business_id, record.cache_id, record.num, record.ext_cache_id, record.business_name)"
                  />
                </template>
                <template slot="official_fee" slot-scope="text,record">
                  {{text * record.num}} = {{text}} * {{record.num}}
                </template>
                <template slot="additional_items" slot-scope="text, record">
                  <a-button shape="circle" icon="plus" size="small" style="margin-right: 10px" @click="addItem(record.business_id, record.cache_id, record.ext_cache_id, record.business_name, record.num)"/>
                  <a-button :disabled="record.ext_cache_id == ''" size="small" type="danger" style="margin-right: 10px" @click="delExt(record.cache_id)">清除</a-button>
                  <span>{{record.ext_price}}</span>
                </template>
                <template slot="sum_offical" slot-scope="text">
                  {{text}}
                </template>
                <template slot="additional_content" slot-scope="text,record">
                  <a-button type="link" @click="showAffix(record.business_id, record.cache_id, record.ext_cache_id, record.business_name, record.num)">查看</a-button>
                </template>
                <template slot="action" slot-scope="text,record">
                  <a-button style="margin-right:5px" type="danger" size="small" @click="delItem(record.contract_id, record.business_id,record.cache_id)">删除</a-button>
                  <a-button type="primary" size="small" @click="addItem(record.business_id, record.cache_id, record.ext_cache_id, record.business_name, record.num)">增项</a-button>
                </template>
                <template slot="footer">
                  官费总计: {{offTotal}}
                  <a-divider type="vertical" />
                  附加项官费: {{extOff}}
                </template>
              </s-table>
            </a-spin>
          </a-checkbox-group>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <a-modal
      title="修改价格"
      v-model="visible2"
      :width="400"
      @ok="onSubmitPrice"
    >
      <a-form-model
        ref="priceForm"
        :model="priceForm"
        :rules="priceRules"
      >
        <a-form-model-item prop="price">
          <a-input placeholder="请输入要修改的价格" v-model="priceForm.price"></a-input>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <ext-modal
      v-if="addVisible"
      :business-id="addForm.business_id"
      :business-name="addForm.business_name"
      :business-num="addForm.business_num"
      :cache-id="addForm.cache_id"
      :ext-cache-id="addForm.ext_cache_id"
      @cancel="onCancel"
      @change="changeExt"
      @delete="onDelete"
    >
    </ext-modal>
    <ext-view
      v-if="extVisible"
      :ext-list="extList"
      @cancel="onCancelExt"
    >
    </ext-view>
  </div>
</template>

<script>
  import { Ellipsis, STable } from '@/components'
  import ExtModal from './ExtModal'
  import ExtView from './ExtView'
  import {
    getBusiness,
    getSelectedBus2,
    postBusinessCache,
    getCacheList,
    delCache,
    delExtCache,
    delAllCache,
    getCacheView,
    addSelectBusiness2,
    postEditPrice,
    delSelectedBus2,
    getSendSms
  } from '@/api/contract'
  import contractColumns from '@/columns/contract'

  export default {
    components: {
      STable,
      Ellipsis,
      ExtModal,
      ExtView
    },
    props: {
      query: {
        type: Object,
        defalut: () => {}
      }
    },
    name: 'Business',
    data () {
      return {
        selectedColumns: contractColumns.selected,
        businessColumns: contractColumns.business,
        selectedData: [], // 现有业务列表
        businessData: [], // 全部业务列表
        businessData2: parameters => {
          return getCacheList(Object.assign(parameters, this.queryParam)).then(res => {
            this.extOff = res.result.total_ext_official_fee
            this.offTotal = res.result.total_official_fee
            this.amount = res.result.total_price
            return res.result
          })
        },
        businessData3: [],
        sourceData: [], // 渲染视图所需列表
        visible: false,
        loading: true,
        loading2: false,
        data: [],
        queryParam: this.query,
        catOptions: {},
        statusOpts: {},
        businessList: {},
        confirmLoading: false,
        riOpts: {},
        epOpts: {},
        atOpts: {},
        totalCount: 0,
        addVisible: false,
        addForm: {},
        extAmount: {},
        extData: {},
        extList: [],
        extVisible: false,
        extOff: 0,
        offTotal: 0,
        amount: 0,
        priceForm: {},
        priceRules: {
          price: [{ required: true, message: '必须要修改的价格', trigger: 'blur' }]
        },
        visible2: false,
        selectedRowKeys: [],
        totalAmount: 0,
        totalOfficial: 0,
        totalAgency: 0,
        totalExt: 0,
        totalAllOff: 0,
        loading3: false
      }
    },
    created () {
      this.getSelected(this.queryParam['contract_id'])
    },
    methods: {
      getSelected (id) {
        const app = this
        const query = {}
        query['filter[contract_id]'] = id
        query['page_size'] = 1000
        getSelectedBus2(query).then(res => {
          if (res.status === 0) {
            app.statusOpts = res.result['status1_opts']
            app.selectedData = res.result['data']
            app.totalCount = res.result.total_count
            app.totalAmount = res.result.totalAmount
            app.totalAgency = res.result.totalAgencyFee
            app.totalOfficial = res.result.OfficialFee
            app.totalAllOff = res.result.totalOfficialFee
            app.totalExt = res.result.extOfficialFee
          }
        })
      },
      getList () {
        const app = this
        app.businessData = []
        getBusiness().then(res => {
          const result = res.result['businesses']
          app.catOptions = res.result['category_opts']
          app.riOpts = res.result['riOpts']
          app.epOpts = res.result['epOpts']
          app.atOpts = res.result['atOpts']
          const target = {}
          if (res.status === 0) {
            for (const k in result) {
              if (result[k].data.hasOwnProperty('')) {
                target[k] = result[k].data[''].data
              } else {
                target[k] = result[k].data
              }
            }
            app.sourceData = target
            app.loading = false
          }
        })
      },
      delItem (tid, bid, id) {
        const app = this
        app.$confirm({
          title: '是否删除',
          onOk: () => {
            app.loading = true
            delCache(tid, bid, id).then(res => {
              if (res.status === 0) {
                for (let i = 0; i < app.businessData3.length; i++) {
                  if (app.businessData[i].business_id === bid) {
                    app.businessData.splice(i, 1)
                  }
                }
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            }).finally(() => {
              app.$refs.table.refresh(true)
              app.loading = false
            })
          }
        })
      },
      addItem (id, cid, ecid, name, num) {
        this.addForm['business_id'] = id
        this.addForm['business_name'] = name
        this.addForm['business_num'] = num
        this.addForm['cache_id'] = cid
        this.addForm['ext_cache_id'] = ecid
        this.addVisible = true
      },
      delAllCache (id) {
        delAllCache(id).then(res => {
          console.log(res)
        })
      },
      onCancel (visible) {
        this.addVisible = visible
      },
      onExtView (ext) {
        this.extList = ext
        this.extVisible = true
      },
      onCancelExt (visible) {
        this.extVisible = visible
      },
      onDel (id) {
        const app = this
        app.$confirm({
          title: '确定删除这条业务吗?',
          onOk: () => {
            const params = Object.assign({ contract_id: app.queryParam['contract_id'] }, { cb_id: id })
            delSelectedBus2(params).then(res => {
              if (res.status === 0) {
                app.getSelected(app.queryParam['contract_id'])
                app.$emit('addBusiness')
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            })
          }
        })
      },
      onChange (checkedValues) {
        const app = this
        const data = {}
        app.loading = true
        if (checkedValues.length > app.businessData.length) {
          app.businessData = []
          app.$nextTick(() => {
            app.businessData = checkedValues
          })
          data['contract_id'] = this.queryParam['contract_id']
          data['business_id'] = checkedValues[checkedValues.length - 1].business_id
          data['cache[business_name]'] = checkedValues[checkedValues.length - 1].name
          data['cache[category]'] = checkedValues[checkedValues.length - 1].category
          // data['cache[price]'] = 0
          data['cache[official_fee]'] = checkedValues[checkedValues.length - 1].official_fee
          postBusinessCache(data).then(res => {
            if (res.status === 0) {
              app.$refs.table.refresh(true)
            } else {
              app.$warning({
                title: res.msg
              })
            }
          }).finally(() => {
            app.loading = false
          })
        } else {
          app.loading = true
          for (let i = 0; i < app.businessData.length; i++) {
            let isExist = false
            for (let j = 0; j < checkedValues.length; j++) {
              if (app.businessData[i].business_id === checkedValues[j].business_id) {
                isExist = true
                break
              }
            }
            if (!isExist) {
              delCache(this.queryParam['contract_id'], app.businessData[i].business_id, '').then(res => {
                if (res.status === 0) {
                  app.businessData.splice(i, 1)
                  app.businessData3.splice(i, 1)
                  app.$refs.table.refresh(true)
                } else {
                  app.$warning({
                    title: res.msg
                  })
                }
              }).finally(() => {
                app.loading = false
              })
            }
          }
        }
      },
      handleChange (value, id, cid) {
        const app = this
        const data = {}
        data['contract_id'] = app.queryParam['contract_id']
        data['business_id'] = id
        data['cache_id'] = cid
        data['cache[price]'] = value
        postBusinessCache(data).then(res => {
          if (res.status === 0) {
            app.$refs.table.refresh()
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      handleChange2 (value, id, cid, num, eid, name) {
        const app = this
        const data = {}
        data['contract_id'] = app.queryParam['contract_id']
        data['business_id'] = id
        data['cache_id'] = cid
        data['cache[num]'] = value
        if (value < num) {
          if (eid !== '') {
            app.$confirm({
              title: '必须删除对应增项后才可以修改数量',
              onOk: () => {
                app.addItem(id, cid, eid, name, num)
              }
            })
          } else {
            postBusinessCache(data).then(res => {
              if (res.status === 0) {
                app.$refs.table.refresh()
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            })
          }
        } else {
          postBusinessCache(data).then(res => {
            if (res.status === 0) {
              app.$refs.table.refresh()
            } else {
              app.$warning({
                title: res.msg
              })
            }
          })
        }
      },
      onSelectChange (selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      businessModal () {
        delAllCache(this.queryParam['contract_id']).then(res => {
        }).finally(() => {
          this.visible = true
          this.getList()
        })
      },
      onSubBusiness () {
        const app = this
        const businesses = {}
        const arr = []
        let ext = {}
        getCacheList(app.queryParam).then(res => {
          for (let i = 0; i < res.result.data.length; i++) {
            for (const k in res.result.data[i]) {
              if (k === 'official_fee') {
                businesses['bs[' + res.result.data[i].business_id + ']' + '[official_fee]'] = res.result.data[i][k]
              }
              if (k === 'num') {
                businesses['bs[' + res.result.data[i].business_id + ']' + '[num]'] = res.result.data[i][k]
              }
              if (k === 'price') {
                businesses['bs[' + res.result.data[i].business_id + ']' + '[price]'] = res.result.data[i][k]
              }
            }
          }
          businesses['contract_id'] = app.queryParam['contract_id']
          for (const k in app.extData) {
            arr.push(k)
          }
          for (let i = 0; i < arr.length; i++) {
            ext = Object.assign(ext, app.extData[arr[i]])
          }
          addSelectBusiness2(Object.assign(ext, businesses)).then(res => {
            if (res.status === 0) {
              app.getSelected(app.queryParam['contract_id'])
              app.$emit('addBusiness')
            } else {
              app.$warning({
                title: res.msg
              })
            }
          })
        })
      },
      showAffix (id) {
        const app = this
        getCacheView(id).then(res => {
          if (res.status === 0) {
            console.log(res)
          } else {
           app.$warning({
             title: '暂未添加附加项'
           })
          }
        })
      },
      changeExt (data, id) {
        this.extData[id] = data
        this.$refs.table.refresh(true)
      },
      onDelete (data, id, cid, num) {
        const app = this
        const data2 = {}
        data2['contract_id'] = app.queryParam['contract_id']
        data2['business_id'] = id
        data2['cache_id'] = cid
        data2['cache[num]'] = num
        this.extData[id] = data
        postBusinessCache(data2).then(res => {
          if (res.status === 0) {
            app.$refs.table.refresh(true)
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      batchFee (type, ids) {
        const app = this
        app.priceForm = {}
        if (type === 1) {
          app.priceForm['cb_ids'] = ids
        } else {
          app.priceForm['cb_ids'] = app.selectedRowKeys
        }
        app.priceForm['contract_id'] = app.queryParam['contract_id']
        app.visible2 = true
      },
      onSubmitPrice () {
        const app = this
        const data = {}
        const params = {}
        app.$refs.priceForm.validate(valid => {
          if (valid) {
            data['price'] = app.priceForm.price
            params['contract_id'] = app.priceForm.contract_id
            params['cb_ids[]'] = app.priceForm.cb_ids
            postEditPrice(data, params).then(res => {
              if (res.status === 0) {
                app.getSelected(app.queryParam['contract_id'])
                app.$emit('addBusiness')
                app.visible2 = false
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            })
          } else {
            return false
          }
        })
      },
      delExt (id) {
        const app = this
        app.$confirm({
          title: '确定清楚当前业务的附加项目吗',
          onOk: () => {
            delExtCache(id).then(res => {
              if (res.status === 0) {
                app.$refs.table.refresh(true)
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            })
          }
        })
      },
      sendSms () {
        const app = this
        app.loading3 = true
        getSendSms(app.queryParam['contract_id']).then(res => {
          if (res.status === 0) {
            app.$confirm({
              title: '向' + res.result.phone + '发送一条合同确认短信',
              okText: '发送',
              onOk: () => {
                app.loading3 = false
                // postSendSms().then(sms => {
                //   if (sms.status === 0) {
                //     app.loading3 = false
                //   } else {
                //     app.$warning({
                //       title: sms.msg
                //     })
                //     app.loading3 = false
                //   }
                // })
              }
            })
          } else {
            app.$warning({
              title: res.msg
            })
            app.loading3 = false
          }
        })
      }
    }
  }
</script>

<style scoped>
  .business-checkbox-group {
    height: 300px;
    overflow-y: scroll;
    overflow-x: hidden
  }
  .group-name {
    background-color: #eee;
    font-weight: bold;
    padding: 5px 10px
  }
  .b_group {
    width: 100%;
    padding: 15px 15px 0 15px
  }
  .tb-footer {
    text-align: right;
    font-size: 18px;
    font-weight: bold;
    color: #f00
  }
  .tb-footer2 {
    text-align: right;
    font-size: 14px;
    color: #f00
  }
</style>
